/**
 * 
 */
package wpmp.security.client.utils;

import javax.servlet.http.HttpServletRequest;

import wpmp.security.client.internal.PmpUserUtils;
import wpmp.utils.quickbean.User;

/**
 * @author Wayne.Wang<5waynewang@gmail.com>
 * @since 2:06:31 PM Dec 4, 2013
 */
public class UserUtils {

	// private static Log LOG = LogFactory.getLog(UserUtils.class);

	/**
	 * 获取当前系统登录用户帐号
	 * 
	 * @param req
	 * @return
	 */
	public static String getLoginName(HttpServletRequest req) {
		final User user = getUser(req);
		return user != null ? user.getLoginName() : null;
	}

	/**
	 * 获取当前系统登录用户姓名
	 * 
	 * @param req
	 * @return
	 */
	public static String getUserName(HttpServletRequest req) {
		final User user = getUser(req);
		return user != null ? user.getUserName() : null;
	}

	/**
	 * 获取当前系统登录用户
	 * 
	 * @param req
	 * @return
	 */
	public static User getUser(HttpServletRequest req) {
		return PmpUserUtils.get(req);
	}

	/**
	 * 根据登录名获取用户
	 * 
	 * @param loginName 登录名
	 * @return
	 */
	public static User getUser(String loginName) {
		return PmpUserUtils.getByLoginName(loginName);
	}

	/**
	 * 根据登录名获取用户姓名
	 * 
	 * @param loginName 登录名
	 * @return
	 */
	public static String getUserName(String loginName) {
		return getUserName(loginName, loginName);
	}

	/**
	 * 根据登录名获取用户姓名
	 * 
	 * @param loginName 登录名
	 * @param defaultUserName 找不到则默认此值
	 * @return
	 */
	public static String getUserName(String loginName, String defaultUserName) {
		User user = getUser(loginName);
		return user == null ? defaultUserName : user.getUserName();
	}

	/**
	 * 判断当前系统登录用户是否是管理员
	 * 
	 * @param req
	 * @return
	 */
	public static boolean isAdmin(HttpServletRequest req) {
		return isAdmin(getUser(req));
	}

	/**
	 * 判断指定用户是否是管理员
	 * 
	 * @param loginName
	 * @return
	 */
	public static boolean isAdmin(String loginName) {
		return isAdmin(getUser(loginName));
	}

	/**
	 * 判断指定用户是否是管理员
	 * 
	 * @param user
	 * @return
	 */
	public static boolean isAdmin(User user) {
		return user == null ? false : user.isAdmin();
	}
}
